package com.wise.service.cms.content.applicationcolumn;

import cn.hutool.core.collection.CollectionUtil;
import com.wise.entities.content.ApplicationArticle;
import com.wise.entities.content.ApplicationColumn;
import com.wise.framework.dao.SqlSelect;
import com.wise.framework.daomodel.DaoList;
import com.wise.framework.daomodel.DaoOne4Update;
import com.wise.framework.daomodel.DaoRemove;
import com.wise.summer.v1.cms.content.applicationcolumn.DeleteApplicationColumnById;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import java.util.List;
import java.util.stream.Collectors;

import static com.wise.model.content.ContentErrMsgs.Err_001600250014;

/**
 * @author hejiawei
 * @date 2020/10/27 11:10
 */
@Service
@SummerService
public class DeleteApplicationColumnService implements SummerServiceBean<DeleteApplicationColumnById> {

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void sum(DeleteApplicationColumnById model) {
        ApplicationColumn applicationColumn = DaoOne4Update.s(ApplicationColumn.class, model.getId(), Err_001600250014);
        DaoRemove.s(applicationColumn);

        //删除对应文章
        SqlSelect sql = SqlSelect.selec("*");
        sql.fromClassName(ApplicationArticle.class);
        sql.eq(ApplicationArticle._columnId, model.getId());
        List<ApplicationArticle> articleList = DaoList.s(ApplicationArticle.class, sql);

        List idList = articleList
                .stream()
                .map(ApplicationArticle::getId)
                .collect(Collectors.toList());

        if (CollectionUtil.isNotEmpty(idList)) {
            MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource();
            mapSqlParameterSource.addValue("id", idList);
            namedParameterJdbcTemplate.update("delete from application_article where id in (:id)", mapSqlParameterSource);
        }
        model.setSummerResult(Boolean.TRUE);
    }
}
